<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <el-form-item label="本月投入资金数额（万元）" prop="personCost">
        <el-input v-model="dataForm.personCost" placeholder="请输入金额"></el-input>
      </el-form-item>
      <!--<el-form-item>-->
        <!--<label>本月销售情况</label>-->
      <!--</el-form-item>-->
      <el-form-item label="本月销售营业额（万元）" prop="manageCost">
        <el-input v-model="dataForm.manageCost" placeholder="请输入金额"></el-input>
      </el-form-item>
      <el-form-item>
        <label>
      证明材料（以附件形式上传相关证明材料的扫描件）
        </label>
      <!--</el-form-item>-->
      <!--<el-form-item label="证明材料" prop="anotherCost">-->
        <!--<el-input v-model="dataForm.anotherCost" placeholder="请输入金额"></el-input>-->
      <!--</el-form-item>-->
      <!--start-->
      <!--<el-form-item>-->
      <el-upload
        class="upload-demo"
        action="https://jsonplaceholder.typicode.com/posts/"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :before-remove="beforeRemove"
        multiple
        :limit="3"
        :on-exceed="handleExceed"
        :file-list="fileList">
        <el-button size="small" type="primary">点击上传</el-button>
        <!--<div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>-->
      </el-upload>
      </el-form-item>
      <!--end-->
      <el-form-item label="本月利润总额（万元）" prop="areaCost">
        <el-input v-model="dataForm.areaCost" placeholder="请输入金额"></el-input>
      </el-form-item>
      <el-form-item>
        <label>
          证明材料（以附件形式上传相关证明材料的扫描件）
        </label>
        <el-upload
          class="upload-demo"
          action="https://jsonplaceholder.typicode.com/posts/"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          multiple
          :limit="3"
          :on-exceed="handleExceed"
          :file-list="fileList">
          <el-button size="small" type="primary">点击上传</el-button>
          <!--<div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>-->
        </el-upload>
      </el-form-item>
      <el-form-item label="本月上缴税总额（万元）" prop="areaCost">
        <el-input v-model="dataForm.areaCost" placeholder="请输入金额"></el-input>
      </el-form-item>
      <el-form-item>
        <label>
          证明材料（以附件形式上传相关证明材料的扫描件）
        </label>
        <el-upload
          class="upload-demo"
          action="https://jsonplaceholder.typicode.com/posts/"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          multiple
          :limit="3"
          :on-exceed="handleExceed"
          :file-list="fileList">
          <el-button size="small" type="primary">点击上传</el-button>
          <!--<div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>-->
        </el-upload>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { isMobile } from '@/utils/validate'
  export default {
    data () {
      var validateMobile = (rule, value, callback) => {
        if (!isMobile(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        baseList: [],
        dataForm: {
          id: 0,
          personCost: '',
          manageCost: '',
          anotherCost: '',
          areaCost: '',
          costStartTime: '',
          costEndTime: '',
          baseId: ''
        },
        dataRule: {
          personCost: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          manageCost1: [
            { required: true, message: '手机号不能为空', trigger: 'blur' },
            { validator: validateMobile, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.visible = true
        this.dataForm.id = id || 0
        this.$http({
          url: this.$http.adornUrl(`/innovate/base/info/list`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.baseList = data.page.list
          }
        })
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/innovate/base/cost/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.personCost = data.cost.personCost
                this.dataForm.manageCost = data.cost.manageCost
                this.dataForm.anotherCost = data.cost.anotherCost
                this.dataForm.areaCost = data.cost.areaCost
                this.dataForm.costStartTime = new Date(data.cost.costStartTime)
                this.dataForm.costEndTime = new Date(data.cost.costEndTime)
                this.dataForm.baseId = data.cost.baseId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/innovate/base/cost/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'costId': this.dataForm.id || undefined,
                'personCost': this.dataForm.personCost,
                'manageCost': this.dataForm.manageCost,
                'anotherCost': this.dataForm.anotherCost,
                'areaCost': this.dataForm.areaCost,
                'costStartTime': Number(this.dataForm.costStartTime),
                'costEndTime': Number(this.dataForm.costEndTime),
                'baseId': this.dataForm.baseId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<!--文件上传模块-->
<!--<script>
  export default {
    data() {
      return {
        fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}]
      };
    },
    methods: {
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file);
      },
      handleExceed(files, fileList) {
        this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
      },
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${ file.name }？`);
      }
    }
  }
</script>-->
